package jssia.database.mysql.columns;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;


public class TimeColumn extends Column {

    private static final int MAX_HOUR = 838;
    private static final int MAX_MINUTE_AND_SECOND = 59;

    public Time get(ResultSet rs) throws SQLException {
        return rs.getTime(fullyQualifiedName);
    }

    public void set(PreparedStatement ps, int index, int hour, int minute, int second) throws SQLException {
        if (hour < -MAX_HOUR || hour > MAX_HOUR)
            throw new IllegalArgumentException("Invalid hour of " + hour + ".  Must be between -" + MAX_HOUR + " and " + MAX_HOUR);
        if (minute < -MAX_MINUTE_AND_SECOND || minute > MAX_MINUTE_AND_SECOND)
            throw new IllegalArgumentException("Invalid minute of " + minute + ".  Must be between -" + MAX_MINUTE_AND_SECOND + " and " + MAX_MINUTE_AND_SECOND);
        if (second < -MAX_MINUTE_AND_SECOND || second > MAX_MINUTE_AND_SECOND)
            throw new IllegalArgumentException("Invalid second of " + second + ".  Must be between -" + MAX_MINUTE_AND_SECOND + " and " + MAX_MINUTE_AND_SECOND);

        final String time = new StringBuilder().append(hour).append(":").append(minute).append(":").append(second).toString();
        System.out.println(time);
        ps.setString(index, time);

    }
    public void set(PreparedStatement ps, int index, long millis) throws SQLException {
        ps.setTime(index, new java.sql.Time(millis));
    }

}
